package cn.org.alan.exam.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.org.alan.exam.common.exception.ServiceRuntimeException;
import cn.org.alan.exam.mapper.JobMapper;
import cn.org.alan.exam.mapper.UserJobDetailsMapper;
import cn.org.alan.exam.model.entity.Job;
import cn.org.alan.exam.model.entity.UserJobDetails;
import cn.org.alan.exam.model.vo.job.JobDetailsVo;
import cn.org.alan.exam.model.vo.job.TeacherJobPageVo;
import cn.org.alan.exam.model.vo.job.UserJobDetailsVo;
import cn.org.alan.exam.service.IJobService;
import cn.org.alan.exam.utils.SecurityUtil;
import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 说明：
 *
 * @Author Alan
 * @Version 1.0
 * @Date 2025/4/3 9:31 PM
 */
@Service
public class JobServiceImpl implements IJobService {
    @Resource
    private JobMapper jobMapper;
    @Resource
    private UserJobDetailsMapper userJobDetailsMapper;
    @Override
    public Job add(Job job) {
        Integer userId = SecurityUtil.getUserId();
        job.setId(null);
        job.setUserId(userId);
        int insert = jobMapper.insert(job);
        if(insert>0){
            return job;
        }
        throw new ServiceRuntimeException("添加作业错误");
    }

    @Override
    public Integer delete(Integer jobId) {
        int delete = jobMapper.deleteById(jobId);
        if(delete>0){
            return jobId;
        }
        throw new ServiceRuntimeException("删除作业失败");
    }

    @Override
    public Job update(Job job) {
        int update = jobMapper.updateById(job);
        if(update>0){
            return job;
        }
        throw new ServiceRuntimeException("修改作业失败");
    }

    @Override
    public Page getTeacherJobPage(Integer pageNum, Integer pageSize, String title,Integer gradeId) {
        Integer userId = SecurityUtil.getUserId();
        Page<TeacherJobPageVo> page = new Page<>(pageNum,pageSize);
        page = jobMapper.getTeacherJobPage(page,userId,title,gradeId);
        return page;
    }

    @Override
    public Page getJobDetails(Integer pageNum, Integer pageSize,Integer jobId) {
        Page<JobDetailsVo> page = new Page<>(pageNum,pageSize);
        return userJobDetailsMapper.getJobDetails(page,jobId);
    }

    @Override
    public UserJobDetailsVo getUserJobDetails(Integer jobId,Integer userId) {
        Integer roleCode = SecurityUtil.getRoleCode();
        if(roleCode==1){
            userId = SecurityUtil.getUserId();
        }
        return userJobDetailsMapper.getUserJobDetails( jobId, userId);
    }

    @Override
    public void mark(Integer jobId, Integer userId, Double score) {
        LambdaQueryWrapper<UserJobDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserJobDetails::getJobId,jobId)
                .eq(UserJobDetails::getUserId,userId);
        UserJobDetails userJobDetails = userJobDetailsMapper.selectOne(queryWrapper);
        if(userJobDetails.getState()!=0){
            throw new ServiceRuntimeException("已打分无法打分");
        }
        LambdaUpdateWrapper<UserJobDetails> query = new LambdaUpdateWrapper<>();
        query.eq(UserJobDetails::getUserId,userId)
                .eq(UserJobDetails::getJobId,jobId)
                .set(UserJobDetails::getScore,score)
                .set(UserJobDetails::getState,1);
        int update = userJobDetailsMapper.update(query);
        if(update<1){
            throw new ServiceRuntimeException("打分失败");
        }
    }
}
